#include "KPServer.h"

#include "KPHelper.h"

KPServer::KPServer() :
		m_state(IDLE),
		m_GLCommandsNumber(0),
		m_client(-1)
{
}

KPServer::~KPServer()
{
	clearGLCommandsList();

	for (int i = 0; i < MAX_PROGRAMS_NUMBER; i++)
	{
		m_pProgramsList[i].clearData();
	}
	for (int i = 0; i < MAX_SHADERS_NUMBER; i++)
	{
		m_pShadersList[i].clearData();
	}
	for (int i = 0; i < MAX_TEXTURES_NUMBER; i++)
	{
		m_pTexturesList[i].clearData();
	}
}

void KPServer::clearGLCommandsList()
{
	for (int i = 0; i < m_GLCommandsNumber; i++)
	{
		m_pGLCommandsList[i].clearData();
	}
	m_GLCommandsNumber = 0;
}

ServerState KPServer::getState()
{
	KPHelper::LockMutex0();
	ServerState state = m_state;
	KPHelper::UnlockMutex0();
	return state;
}

void KPServer::setState(ServerState state)
{
	KPHelper::LockMutex0();
	m_state = state;
	KPHelper::UnlockMutex0();
}

void KPServer::setClient(int client)
{
	m_client = client;
}

//========================================================================================
void KPServer::onGL_GenTextures(GLsizei n, GLuint* textures)
{
	if (getState() == CAPTURING)
	{
		KPMessage& msg = m_pGLCommandsList[m_GLCommandsNumber++];
		msg.clearData();
	
		msg.m_type = KMT_GL_GEN_TEXTURES;
	
		const char* buffer = "TEST onGL_GenTextures";
		int len = strlen(buffer);
		msg.m_length = len;
		msg.m_pData = new char[len];
		memcpy(msg.m_pData, buffer, len);
	}
}

void KPServer::onGL_DeleteTextures(GLsizei n, const GLuint* textures)
{
}

void KPServer::onGL_BindTexture (GLenum target, GLuint texture)
{
	if (getState() == CAPTURING)
	{
		KPMessage& msg = m_pGLCommandsList[m_GLCommandsNumber++];
		msg.clearData();
	
		msg.m_type = KMT_GL_BIND_TEXTURE;
	
		const char* buffer = "TEST onGL_BindTexture";
		int len = strlen(buffer);
		msg.m_length = len;
		msg.m_pData = new char[len];
		memcpy(msg.m_pData, buffer, len);
	}
}

void KPServer::onGL_DrawArrays(GLenum mode, GLint first, GLsizei count)
{
	if (getState() == CAPTURING)
	{
		KPMessage& msg = m_pGLCommandsList[m_GLCommandsNumber++];
		msg.clearData();
	
		msg.m_type = KMT_GL_DRAW_ARRAYS;
	
		const char* buffer = "TEST onGL_DrawArrays";
		int len = strlen(buffer);
		msg.m_length = len;
		msg.m_pData = new char[len];
		memcpy(msg.m_pData, buffer, len);
	}
}

void KPServer::onSwapBuffers()
{
	KPHelper::LockMutex0();

	if (m_state == PRE_CAPTURE)
	{
		LOGD("[KPServer::onSwapBuffers()] PRE_CAPTURE");
		for (int i = 0; i < MAX_PROGRAMS_NUMBER; i++) if (m_pProgramsList[i].getId())
		{
			KPHelper::sendGLObjectToClient(m_client, m_pProgramsList[i]);
		}
		for (int i = 0; i < MAX_SHADERS_NUMBER; i++) if (m_pShadersList[i].getId())
		{
			KPHelper::sendGLObjectToClient(m_client, m_pShadersList[i]);
		}
		for (int i = 0; i < MAX_TEXTURES_NUMBER; i++) if (m_pTexturesList[i].getId())
		{
			KPHelper::sendGLObjectToClient(m_client, m_pTexturesList[i]);
		}
		
		clearGLCommandsList();

		m_state = CAPTURING;
	}
	else if (m_state == CAPTURING)
	{
		LOGD("[KPServer::onSwapBuffers()] CAPTURING m_GLCommandsNumber = %d m_client = %d", m_GLCommandsNumber, m_client);

		for (int i = 0; i < m_GLCommandsNumber; i++)
		{
			KPHelper::sendMessageToClient(m_client, m_pGLCommandsList[i]);
		}

		KPHelper::sendFinishCaptureMessageToClient(m_client);
		KPHelper::closeSocket(m_client);

		clearGLCommandsList();

		m_state = IDLE;
	}

	KPHelper::UnlockMutex0();
}
